package nb.photoviewer.ImageEditor;

import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Cropper extends BorderPane{
    private Image originalImage;
    private ImageView imageView;
    private Rectangle cropBounds;
    private double startX = 0;
    private double startY = 0;

    //Cropper是一个BorderPane
    public Cropper(Image image) {

        this.originalImage = image;
        this.imageView = new ImageView(originalImage);
        this.imageView.setFitHeight(600);
        this.imageView.setFitWidth(600); // 设置 imageView 的宽度
        this.imageView.setPreserveRatio(true); // 保持原始比例
        this.setCenter(imageView);

        cropBounds = new Rectangle(100, 100); // 初始裁剪区域⼤⼩
        cropBounds.setStroke(Color.BLUE);
        cropBounds.setFill(Color.TRANSPARENT); // 你可以使⽤半透明颜⾊以便看到裁剪下⽅的图⽚
        this.getChildren().add(cropBounds);

        // 处理矩形的⿏标事件来允许调整裁剪区域...
        addMouseEvents1();  //在矩形框内可以开始裁剪
        addMouseEvents2();  //在矩形框外可以开始裁剪

        // 设置 Cropper 的背景颜色为哑黑色
        this.setStyle("-fx-background-color: #333333;");
    }
    public Image  performCrop() {
        try{
            double scaleFactor = originalImage.getWidth() / imageView.getBoundsInParent().getWidth();
            int x = (int) (cropBounds.getX() * scaleFactor);
            int y = (int) (cropBounds.getY() * scaleFactor);
            int width = (int) (cropBounds.getWidth() * scaleFactor);
            int height = (int) (cropBounds.getHeight() * scaleFactor);
            WritableImage croppedImage = new WritableImage(originalImage.getPixelReader(), x, y, width, height);
            imageView.setImage(croppedImage); // 更新imageView以显⽰剪切的图像
            // 其他任何需要在裁剪后执⾏的代码...
            return croppedImage;
        } catch (Exception ex) {
           ex.printStackTrace(); // 在控制台打印异常信息
           // 处理异常，你可以根据实际情况来处理
           return null;
        }
    }
    // 其他⽅法，⽐如调整裁剪区域、添加⿏标事件处理器等...

    //在矩形框内开始裁剪
    private void addMouseEvents1() {
        EventHandler<MouseEvent> mousePressedHandler = event -> {
            startX = event.getX();
            startY = event.getY();
            // 限制裁剪起点坐标在图片范围内
            startX = Math.max(0, Math.min(startX, imageView.getBoundsInParent().getWidth()));
            startY = Math.max(0, Math.min(startY, imageView.getBoundsInParent().getHeight()));
            cropBounds.setX(startX);
            cropBounds.setY(startY);
            cropBounds.setWidth(0);
            cropBounds.setHeight(0);
        };

        EventHandler<MouseEvent> mouseDraggedHandler = event -> {
            double mouseX = event.getX();
            double mouseY = event.getY();
            // 计算裁剪矩形的位置和大小
            double x = Math.min(startX, mouseX);
            double y = Math.min(startY, mouseY);
            double width = Math.abs(mouseX - startX);
            double height = Math.abs(mouseY - startY);
            // 更新裁剪矩形的位置和大小
            cropBounds.setX(x);
            cropBounds.setY(y);
            cropBounds.setWidth(width);
            cropBounds.setHeight(height);
        };

        EventHandler<MouseEvent> mouseReleasedHandler = event -> {
            // 确保裁剪区域不超出图像范围
            double imageWidth = imageView.getBoundsInParent().getWidth();
            double imageHeight = imageView.getBoundsInParent().getHeight();
            double x = cropBounds.getX();
            double y = cropBounds.getY();
            double width = cropBounds.getWidth();
            double height = cropBounds.getHeight();

            // 确保裁剪区域不超出图像范围的左边和上方
            if (x < 0) {
                width += x; // 减去超出的部分
                x = 0; // 将 x 设为 0
            }
            if (y < 0) {
                height += y; // 减去超出的部分
                y = 0; // 将 y 设为 0
            }

            // 更新裁剪区域的位置和大小
            cropBounds.setX(x);
            cropBounds.setY(y);
            cropBounds.setWidth(Math.min(width, imageWidth - x));
            cropBounds.setHeight(Math.min(height, imageHeight - y));
        };

        imageView.setOnMousePressed(mousePressedHandler); // 在图片区域内响应鼠标按下事件
        cropBounds.setOnMousePressed(mousePressedHandler); // 在裁剪区域内也响应鼠标按下事件
        cropBounds.setOnMouseDragged(mouseDraggedHandler);
        cropBounds.setOnMouseReleased(mouseReleasedHandler);

    }
    //在矩形框外开始裁剪
   private void addMouseEvents2() {
        EventHandler<MouseEvent> mousePressedHandler = event -> {
            startX = event.getX();
            startY = event.getY();
            // 限制裁剪起点坐标在图片范围内
            startX = Math.max(0, Math.min(startX, imageView.getBoundsInParent().getWidth()));
            startY = Math.max(0, Math.min(startY, imageView.getBoundsInParent().getHeight()));
            cropBounds.setX(startX);
            cropBounds.setY(startY);
            cropBounds.setWidth(0);
            cropBounds.setHeight(0);
        };

        EventHandler<MouseEvent> mouseDraggedHandler = event -> {
            double mouseX = event.getX();
            double mouseY = event.getY();
            // 计算裁剪矩形的位置和大小
            double x = Math.min(startX, mouseX);
            double y = Math.min(startY, mouseY);
            double width = Math.abs(mouseX - startX);
            double height = Math.abs(mouseY - startY);
            // 更新裁剪矩形的位置和大小
            cropBounds.setX(x);
            cropBounds.setY(y);
            cropBounds.setWidth(width);
            cropBounds.setHeight(height);
        };

        EventHandler<MouseEvent> mouseReleasedHandler = event -> {
            // 确保裁剪区域不超出图像范围
            double imageWidth = imageView.getBoundsInParent().getWidth();
            double imageHeight = imageView.getBoundsInParent().getHeight();

            double x = cropBounds.getX();
            double y = cropBounds.getY();
            double width = cropBounds.getWidth();
            double height = cropBounds.getHeight();

            // 确保裁剪区域不超出图像范围的左边和上方
            if (x < 0) {
                width += x; // 减去超出的部分
                x = 0; // 将 x 设为 0
            }
            if (y < 0) {
                height += y; // 减去超出的部分
                y = 0; // 将 y 设为 0
            }
            // 更新裁剪区域的位置和大小
            cropBounds.setX(x);
            cropBounds.setY(y);
            cropBounds.setWidth(Math.min(width, imageWidth - x));
            cropBounds.setHeight(Math.min(height, imageHeight - y));
        };

        imageView.setOnMousePressed(mousePressedHandler); // 在图片区域内响应鼠标按下事件
        imageView.setOnMouseDragged(mouseDraggedHandler); // 在图片区域内响应鼠标拖拽事件
        imageView.setOnMouseReleased(mouseReleasedHandler); // 在图片区域内响应鼠标释放事件
    }


}
